/**
 * 获取一级类别
 */
 function getMainType(){
	 var url="/BeautyClub/category/get_category.do?parent=0";
		var selectNode = $(".category-first");		
		jQuery.post(url,{},function(data)
		{			
			var innerHtm = "<option value='0' selected >--请选择--</option>";
			if(data!=null && data!="" && data.length>0)
			{				
				for(var i=0;i<data.length;i++)
				{
					var typeId = data[i].cId==null?"":data[i].cId; 
					var typeName = data[i].cName==null?"":data[i].cName; 
					innerHtm+="<option value='"+typeId+"'>"+typeName+"</option>";					
				}
			}
			selectNode.html(innerHtm);
	     },"json");
}

/**
 * 获取二级类别
 */
 function getChildType(){
	var id=$(".category-first").val();
	if(id=='0'){
		$(".category-second").html("<option value='0' selected >--请选择--</option>");
		return false;
	}
	
	 var url="/BeautyClub/category/get_category.do?parent=";
	 url += id;
	 var selectNode = $(".category-second");
	 jQuery.post(url,{},function(data)
	 {
			var innerHtm = "<option value='0' selected >--请选择--</option>";
			if(data!=null && data!="" && data.length>0)
			{
				for(var i=0;i<data.length;i++)
				{
					var typeId = data[i].cId==null?"":data[i].cId; 
					var typeName = data[i].cName==null?"":data[i].cName; 
					innerHtm+="<option value='"+typeId+"'>"+typeName+"</option>";
				}
			}
			selectNode.html(innerHtm);			
	 },"json");
}
 
/**
 * 获取类别信息
 */
function getCategory(){	
	var divNode = $(".category-list");
	var url = "/BeautyClub/category/category_list.do";	
	jQuery.post(url,{},function(data)
	{
		var innerHtm = "";
		if(data!=null && data!="" && data.length>0)
		{			
			for(var i=0;i<data.length;i++)
			{				
				var cId = data[i].cId==null?"":data[i].cId;
				var cName = data[i].cName==null?"":data[i].cName;
				var parentId = data[i].parentId==null?"":data[i].parentId;
				var introduce = data[i].introduce==null?"":data[i].introduce;
				var time = data[i].time==null?"":new Date(data[i].time);
				innerHtm+="<a class='list-group-item' style='cursor:default'>";
				innerHtm+="<input type='hidden' value='"+cId+"'/>";				
				if(parentId=="0"){
					innerHtm+="<span class='fa fa-star text-yellow mrm mlm'></span>";
					innerHtm+="<span style='min-width: 120px; display: inline-block;' class='name'>"+cName+"</span>";
					innerHtm+="<span>一级类别</span>&nbsp;&nbsp;";
				}else{
					innerHtm+="<span class='fa fa-star mrm mlm'></span>";
					innerHtm+="<span style='min-width: 120px; display: inline-block;' class='name'>"+cName+"</span>";
					innerHtm+="<span>二级类别</span>&nbsp;&nbsp;";
				}				
				innerHtm+="<span style='width:70%; display: inline-block;'><span class='pull-right mrl'>";
				//innerHtm+="<i class='fa fa-edit edit-category' style='cursor:pointer' onclick='editCategory(this);'>&nbsp;编辑</i>&nbsp;&nbsp;";
				innerHtm+="<i class='fa fa-trash-o delete-category' style='cursor:pointer' onclick='deleteCategory(this);'>&nbsp;删除</i>";				
				innerHtm+="</span></span>";
				innerHtm+="<span class='badge'>"+time.toLocaleString()+"</span></a>";
			}
		}else{
			innerHtm+="<a class='list-group-item' style='cursor:default'><center><span>未能找到结果！</span></center></a>";
		}
		divNode.html(innerHtm);	
	},"json");
}

//删除类别
function deleteCategory(obj){
	var a = $(obj).parents("a:first");
	var val = a.find("input").val();	
	layer.confirm('您确定要删除吗？', {
        closeBtn: 2,
        title: '提示',
        btn: ['确定', '取消']
    }, function (index, layero){
    	$.ajax({
            url:"/BeautyClub/category/delete_category.do",
            type:'POST',
            dataType:'json',
            contentType:"application/json",
            data:JSON.stringify({"cId":val}),
            async:false,
            success:function(data){
            	if(data==true){
            		a.remove();
            		layer.msg('删除成功！', { icon: 6 });
            	}else{
            		layer.msg('删除失败！', { icon: 5 });
            	}
            },
            error:function(result){
            	layer.msg('删除失败！', { icon: 5 });
            }
        });
    	layer.close(index);
    }, function (index) {
        //取消按钮的回调函数（默认为关闭询问框）
    });
}

